package cae.gui.destination;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import cae.gui.util.CaeJDialog;
import cae.logic.destination.CaeDestinatiosManager;
import cae.logic.destination.CaeGroup;
import cae.logic.destination.CaeGroupManager;


public class CaeDeleteGroupDialog extends CaeJDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel panel = null;
	
	private JComboBox nick;
	private JList list;
	
	public CaeDeleteGroupDialog(JFrame frame, boolean modal)
	{
		
		super(frame, "Eliminar Grupo: ", modal);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		panel = new JPanel();
		getContentPane().add(panel);
		
		panel.setLayout( new GridBagLayout() );
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.insets = new Insets(5,5,5,5);
		destConst.anchor = GridBagConstraints.WEST;
		destConst.fill = GridBagConstraints.HORIZONTAL;
		
		destConst.gridx = 0;
		destConst.gridy = 0;
		panel.add(new JLabel("Nick: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 0;
		
		List<CaeGroup> groups = CaeGroupManager.getInstance().getGroups();
		Object[] o = new Object[groups.size()];
		for (int i = 0; i < groups.size(); i++) {
			o[i] = groups.get(i).getNick();
		}
		nick = new JComboBox(o);
		nick.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) {
		        updateData();
		    }
		});
		nick.setMinimumSize(new Dimension(200, 22));
		nick.setMaximumSize(new Dimension(200, 22));
		nick.setPreferredSize(new Dimension(200, 22));
		panel.add(nick, destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 2;
		list = new JList();
		list.setMinimumSize(new Dimension(200, 150));
		list.setMaximumSize(new Dimension(200, 150));
		list.setPreferredSize(new Dimension(200, 150));
		panel.add(new JScrollPane(list), destConst);

		destConst.gridx = 1;
		destConst.gridy = 3;
		JButton add = new JButton("Eliminar");
		add.addActionListener(this);
		panel.add(add, destConst);
		
		updateData();
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 4, screenSize.height /4);
        
		setResizable(false);
		setAlwaysOnTop(true);
		setVisible(true);
		
		// TODO FOCO
		//FocusManager.getCurrentManager().
		
	}

	protected void updateData() 
	{
		String nickS = (String)nick.getSelectedItem();
		List<String> dests = CaeGroupManager.getInstance().getGroup(nickS).getMembers();
		String[] values = new String[dests.size()];
		
		for(int i = 0; i<dests.size(); i++)
		{
			values[i] = CaeDestinatiosManager.getInstance().getDestinationByAlias(dests.get(i)).getAlias();
		}
	    list.setListData(values);
	}

	public void actionPerformed(ActionEvent e)
	{
		String nickS = (String)nick.getSelectedItem();
		CaeGroup group = CaeGroupManager.getInstance().getGroup(nickS);
        CaeGroupManager.getInstance().deleteGroup(group);
		setVisible(false);
		
	}
}
